package com.juicygames.undoer.fps;

import com.juicygames.framework.GameAction;
import com.juicygames.framework.action.UpdateAction;
import com.juicygames.framework.draw.DrawDepth;
import com.juicygames.framework.draw.DrawService;
import com.juicygames.framework.draw.TextDisplay;
import com.juicygames.framework.gamepart.GameHandler;
import com.juicygames.framework.gamepart.GamePart;

public class FpsDisplayer extends GamePart {
	
	TextDisplay textDisplay;
	
	public FpsDisplayer(GamePart parent) {
		super(parent);
		
		textDisplay = addHandler(new TextDisplay(this));
		textDisplay.setX(5);
		textDisplay.setY(10);
		
		groundHandler(addHandler(new MyUpdateHandler(this)));
		
		addPart(new DrawDepth(this,1));
		getWorld().getPart(DrawService.class).register(textDisplay);
	}

	public TextDisplay getTextDisplay() {
		return textDisplay;
	}
	
	
	class MyUpdateHandler extends GameHandler {
		
		public MyUpdateHandler(GamePart parent) {
			super(parent);
			setActionClassHandled(UpdateAction.class);
		}
		
		public void handle(GameAction action) {
			UpdateAction update = (UpdateAction) action;
			((FpsDisplayer)self).getTextDisplay()
					.setText(Float.toString(update.getFramesPerSecond())+" fps");
		}
		
	}

}
